<template>
	<view class="recharge">
		<view class="recharge-wrap">
			<view>Recharge amount</view>
			<view class="input-wrap">				 
				<!-- <input class="input-box" type="number" placeholder="Please enter the recharge amount" v-model="money" /> -->
				<view v-for="(item,index) in amountList" :key="index" class="amount-item" :class="{cur:index===curAmount}" @click="chooseAmount(index)">{{item}}</view>
			</view>
		</view>
		<view class="address-wrap">
			<view class="title">Payment</view>
			 <radio-group @change="radioChange" class="group">
				<view class="item" v-for="(item,index) in paytype_set" :key="index">
					<view style="display: flex;align-items: center;">
						<img mode="widthFix" :src="interfaceurl+item.icon" class="payicon"/>
						<text class="content">{{item.name}}</text>
					</view>
					<radio class="myradio" color="#F53D46"  :value="''+index" :checked="index==0?true:false"/>				
				</view>
			</radio-group>
		</view>
		<view class="btn" @click="handleRecharge">Submit</view>
	</view>
</template>

<script>
	import fetch from '@/common/fetch.js';
	export default {
		data() {
			return {
				money: '',
				paytype_set: [],
				choose_pay_type:'',
				choose_pay_id:'',
				amountList:[],
				// 所选金额index
				curAmount:''
			}
		},
	computed:{
			interfaceurl(){
				let url = fetch.interfaceUrl()
				return url
			}
		},
		onShow() {
			this.getpaytype();
			this.getAmountList()
		},
		methods: {
			/**获取充值金额列表 */
			getAmountList(){
				fetch.request('/index/mobile/kefu', '', 'get').then(res => {
					let {data} = res;
					this.amountList = data.config.recharge_money.split(',');
				});
			},
			chooseAmount(index){
				this.curAmount = index;
				this.money = this.amountList[index];
			},
			radioChange(e){
				this.choose_pay_type = this.paytype_set[e.target.value].type;
				this.choose_pay_id = this.paytype_set[e.target.value].id;
				console.log(e.target.value,this.choose_pay_type ,this.choose_pay_id)
			},
			handleRecharge() {
				if (this.checkInfo()) {
					if(this.choose_pay_type === 0){
						uni.navigateTo({
							url: './recharge?money=' + this.money
						})
					}else if(this.choose_pay_type === 3){
						this.getUrl()
					}else if(this.choose_pay_type === 2){
						this.getUrl()
					}					
				}
			},
			getUrl(){
				let that = this
				let postdata = {
					amount:that.money,
					id:that.choose_pay_id
				}
				fetch.request('/index/Finance/createOnlinePay', postdata, 'get').then(res => {
						if(res.code === 0){
							if(that.choose_pay_type === 3){
								uni.setStorage({
									key: 'onlinekefu',
									data: res.data.weburl,
									success: function () {
										// 跳webview页面
										uni.navigateTo({
											url:'/pages/consult/index'
										})
									}
								});		
							}
							if(that.choose_pay_type === 2){
								uni.setStorage({
									key: 'payCode',
									data: res.data.weburl,
									success: function () {
										// 跳展示二维码页面
										uni.navigateTo({
											url:'/pages/recharge/code'
										})
									}
								});		
							}
						}else{
							fetch.toast(res.msg)
						}
							
				});
			},
			checkInfo() {
				if (!this.money) {
					fetch.toast('Please enter the recharge amount')
					return false
				}
				return true
			},
			/**
			 * 获取支付方式列表
			 */
			getpaytype() {
				fetch.request('/index/finance/index', '', 'get').then(res => {
					let {
						data
					} = res;
					this.paytype_set = data;
					this.initPayType()
				});
			},
			/*
			* 默认选中第一个支付方式
			*/
		   initPayType(){
			   this.choose_pay_type = this.paytype_set[0].type;
			   this.choose_pay_id = this.paytype_set[0].id;
		   }
		}
	}
</script>

<style lang="less" scoped>
	page,
	.recharge {
		width: 100%;
		min-height: 100%;
	}

	.flex {
		display: flex;
	}

	.address-wrap,
	.recharge-wrap {
		margin: 30rpx 20rpx;
		box-sizing: border-box;
		background: #fff;
		border-radius: 20rpx;
	}
.address-wrap .title{
	padding: 30rpx 50rpx 0 50rpx;
	font-size: 16px;
}
	.recharge-wrap {
		font-size: 16px;
		padding: 30rpx 50rpx;

		.input-wrap {
			font-size: 24px;
			margin-top: 20rpx;
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
			.amount-item{
				height:60rpx;
				line-height: 60rpx;
				text-align: center;
				min-width: 30%;
				background: #fff;
				color: #F53D46;
				font-size: 28rpx;
				border: solid 1rpx #F53D46;
				border-radius: 8rpx;
				margin-left: 20rpx;
				margin-top: 20rpx;
				float: left;
			}
			.amount-item.cur{
				background:#F53D46;
				color: #fff;
			}
		}

		.input-box {
			font-size: 24px;
			margin-left: 30rpx;
		}
	}

	.item {
		padding: 30rpx 50rpx;
		box-sizing: border-box;
		border-bottom: solid 1rpx #f4f4f4;
		font-size: 16px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.payicon{
			width:60rpx;
			background: #eee;
			margin-right: 20rpx;
		}
		.content {			
			display: inline-block;
			color: #8798a3;
		}

		.icon {
			width: 26rpx;
			height: 34rpx;
			vertical-align: middle;
		}
	}
.group{
	width:100%;
}
	.btn {
		color: #fff;
		width: 710rpx;
		margin-left: 20rpx;
		margin-top: 80rpx;
		text-align: center;
		line-height: 80rpx;
		border-radius: 80rpx;
		height: 80rpx;
		background: #F53D46;
	}

</style>
